<template>
  <div class="forecastPlan">
    <TitleColumn name="农作物统计" isColumn />
    <div class="content">
      <h3 class="total">
        <p>种植总面积</p>
        <section>
          <span>5</span>
          <span>4</span>
          <span>4</span>
          <span>6</span>
          <span>.</span>
          <span>7</span>
          <span>2</span>
        </section>
        <p>亩</p>
      </h3>
      <!-- 图表主体 -->
      <div class="chartWarp">
        <div class="chartContent" ref="chartRef"></div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, onMounted } from "vue";
  import TitleColumn from "@/components/TitleColumn";
  import { useECharts } from "@gui-pkg/hooks";
  import * as echarts from 'echarts';

  defineOptions({ name: "ForecastPlan" });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  const state = reactive({
    name: "vue3",
  });

  const initChartData = async() => {
    const chartData = [
      {
        value: 3861.29,
        name: '水稻',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
            offset: 0, color: 'rgba(34, 247, 254, 0.1)'}, {offset: 1, color: 'rgba(34, 247, 254, 1)'}
          ])
        }
      },
      {
        value: 3861.29,
        name: '小麦',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
            offset: 0, color: 'rgba(20, 180, 240, 0.1)'}, {offset: 1, color: 'rgba(20, 180, 240, 1)'}
          ])
        }
      },
      {
        value: 452.22,
        name: '蔬菜',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(1, 1, 0, 0, [{
            offset: 0, color: 'rgba(248, 137, 3, 0.1)'}, {offset: 1, color: 'rgba(248, 137, 3, 1)'}
          ])
        }
      },
      {
        value: 228.02,
        name: '苗木',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 1, 1, 1, [{
            offset: 0, color: 'rgba(74, 219, 108, 0.1)'}, {offset: 1, color: 'rgba(74, 219, 108, 1)'}
          ])
        }
      },
      {
        value: 298.25,
        name: '设施农业',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0, color: 'rgba(250, 214, 46, 0.1)'}, {offset: 1, color: 'rgba(250, 214, 46, 1)'}
          ])
        }
      },
      {
        value: 606.94,
        name: '养殖',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0, color: 'rgba(219, 35, 217, 0.1)'}, {offset: 1, color: 'rgba(219, 35, 217, 1)'}
          ])
        }
      },
    ]

    const chartOption = {
      color: ['#20F8FF', '#06B7FD', '#23DF7B', '#FDD82E', '#FF8A00'],
      grid: {
        top: '0',
        left: '0',
        right: '0',
        bottom: '0',
        containLabel: true,
      },
      legend: {
        orient: 'vertical',
        right: '-6',
        bottom: '50',
        itemGap: 10,
        itemWidth: 10,
        itemHeight: 8,
        itemStyle: {
          // color: []
        },
        textStyle: {
          // 文字的样式
          fontSize: 14, // 可控制每个legend项的间距
          color: '#aaa',
          rich: {
            // 通过富文本rich给每个项设置样式，下面的oneone、twotwo、threethree可以理解为"每一列"的样式
            oneone: {
              color: '#B4DFFF',
              fontSize: 12,
              padding: [0, 2],
              fontWeight: 'bolder',
            },
            twotwo: {
              color: '#B4DFFF',
              fontSize: 12,
              padding: [0, 2],
              fontWeight: 'bolder',
            },
          },
        },
        formatter: (name) => {
          let value; // 遍历拿到数据
          for (let i = 0; i < chartData.length; i++) {
            if (chartData[i].name == name) {
              value = chartData[i].value;
            }
          }
          return `{oneone|${name}} {twotwo|${value}亩}`;
          // 富文本第一列样式应用  富文本第二列样式应用  富文本第三列样式应用
        },
      },
      toolbox: {
        show: false,
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: ['50%', '74%'],
          top: '0',
          left: '-140',
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 12,
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: chartData
        }
      ]
    };
    setOptions(chartOption);
  }

  onMounted(() => {
    initChartData();
  });
</script>

<style lang="less" rel="stylesheet/less" scoped>
.forecastPlan {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  grid-row-gap: 10px;
  box-sizing: border-box;
  background: #02123433;
  backdrop-filter: blur(10px);
  .content {
    width: 100%;
    height: 100%;
    padding: 10px;
    overflow: hidden;
    position: relative;
    .total {
      width: 100%;
      height: 42px;
      padding: 0 20px;
      background: url(./images/bg.png) no-repeat;
      background-size: 100% 100%;
      display: flex;
      align-items: center;
      color: #fff;
      margin-bottom: 14px;
      section {
        display: flex;
        align-items: center;
        span {
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 14px;
          width: 24px;
          height: 24px;
          background: url(./images/num.png) no-repeat;
          background-size: 100% 100%;
          margin-right: 2px;
        }
      }
    }
    .chartWarp {
      width: 100%;
      height: calc(100% - 42px);
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      &::after {
        content: '';
        display: inline-block;
        width: 210px;
        height: 210px;
        background: url(./images/chart_bg.png) no-repeat;
        background-size: contain;
        position: absolute;
        top: 8px;
        left: -14px;
        z-index: -1;
      }
    }
    .chartContent {
      width: 320px;
      height: 226px;
    }
  }
}
</style>
